#! coding:utf-8

import os

# 将源码从src复制到build再执行构建
VariantDir('build', 'src')

GTEST_HOME = '../../googletest/googletest/'
GTEST_INCLUDE = os.path.join(GTEST_HOME, 'include')

# 分析代码覆盖率，GCC编译选项要添加-fprofile-arcs -ftest-coverage

testEnv = Environment(
    ENV = os.environ, # 继承环境变量，防止找不到scons
    CCFLAGS='-ggdb -Wall -I %s' % (GTEST_INCLUDE),  # C/C++公用编译选项，CXXFLAGS为仅适用于指定C++的编译选项
    CFLAGS='-fprofile-arcs -ftest-coverage',        # C编译选项
    LINKFLAGS='-fprofile-arcs -ftest-coverage',
    LIBPATH=GTEST_HOME,
    LIBS=['libgtest', 'pthread' ]
)

sources = ['build/add.c']
testSources = ['build/add_test.cc']

# 处理结果保存在testProg中，其中保存了a.out文件的对象(Scons.Node.FS.File)
testProg = testEnv.Program('a.out', sources + testSources) 
testReport = testEnv.Command('test_report.xml', testProg, "./a.out --gtest_output=xml:${TARGET}")

# gcovr 
#   -x 生成结果为xml格式
#   -r build 从build目录获取覆盖率数据
#   -o 指定输出的报告名
coverageReport = testEnv.Command('coverage.xml', testReport, "gcovr -x -r build -o ${TARGET}")

# 默认构建
Default(coverageReport)
